using System;
using System.Data;

namespace Glpt.Data
{
    /// <summary>
    /// 复合数据流类
    /// </summary>
    public class MultDataReader : IDataReader
    {
        private IDataReader dtread = null;
        private object[] values = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dtread">数据流</param>
        public MultDataReader(IDataReader read)
        {
            dtread = read;
            values = new object[dtread.FieldCount];
        }

        private static object privateObjectLock = new object();
        public bool Read()
        {
            lock (privateObjectLock)
            {
                if (dtread.Read())
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = dtread.GetValue(i);
                    }
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 关闭StreamReader对象和基础流
        /// </summary>
        public void Close()
        {
            if (!dtread.IsClosed) dtread.Close();
        }

        public int FieldCount
        {
            get { return values.Length; }
        }
        public string GetString(int i)
        {
            return values[i] == null ? null : values[i].ToString();
        }

        public object GetValue(int i)
        {
            return values[i];
        }

        public string GetName(int i)
        {
            return dtread.GetName(i);
        }

        public int GetOrdinal(string name)
        {
            return dtread.GetOrdinal(name);
        }

        public int Depth
        {
            get
            {
                return dtread.Depth;
            }
        }

        public bool IsClosed
        {
            get
            {
                return dtread.IsClosed;
            }
        }

        public int RecordsAffected
        {
            get
            {
                return dtread.RecordsAffected;
            }
        }

        public DataTable GetSchemaTable()
        {
            return dtread.GetSchemaTable();
        }

        public bool NextResult()
        {
            return dtread.NextResult();
        }

        public object this[int i]
        {
            get
            {
                return GetValue(i);
            }
        }
        public object this[string name]
        {
            get
            {
                return GetValue(GetOrdinal(name));
            }
        }
        public bool GetBoolean(int i)
        {
            return bool.Parse(GetString(i));
        }
        public byte GetByte(int i)
        {
            return byte.Parse(GetString(i));
        }
        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            return -1;
        }
        public char GetChar(int i)
        {
            return char.Parse(GetString(i));
        }
        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            return -1;
        }
        public IDataReader GetData(int i)
        {
            return dtread.GetData(i);
        }
        public string GetDataTypeName(int i)
        {
            return dtread.GetDataTypeName(i);
        }
        public DateTime GetDateTime(int i)
        {
            return DateTime.Parse(GetString(i));
        }
        public decimal GetDecimal(int i)
        {
            return decimal.Parse(GetString(i));
        }
        public double GetDouble(int i)
        {
            return double.Parse(GetString(i));
        }
        public Type GetFieldType(int i)
        {
            return dtread.GetFieldType(i);
        }
        public float GetFloat(int i)
        {
            return float.Parse(GetString(i));
        }
        public Guid GetGuid(int i)
        {
            return Guid.Parse(GetString(i));
        }
        public short GetInt16(int i)
        {
            return short.Parse(GetString(i));
        }
        public int GetInt32(int i)
        {
            return int.Parse(GetString(i));
        }
        public long GetInt64(int i)
        {
            return long.Parse(GetString(i));
        }
        public int GetValues(object[] values)
        {
            for (int i = 0; i < this.values.Length; i++)
                values[i] = this.values[i];
            return this.values.Length;
        }
        public bool IsDBNull(int i)
        {
            return (GetValue(i) == DBNull.Value);
        }
        public void Dispose()
        {
            try
            {
                GC.Collect();
            }
            catch { }
        }
        ~MultDataReader()
        {
            Dispose();
        }
    }
}
